﻿using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SystemModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Newtonsoft.Json;
using AutoMapper;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Presentation.Controller.SystemModule
{
    public class AccountController : EntityController<Account, string, AccountSearch, IAccountViewModel, IAccountAppService>, IAccountController
    {
        public override string ControllerName
        {
            get { return ControllerConst.ACCOUNT; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public AccountController()
        {
            this.Model = ModelFactory.CreateModel<IAccountViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name="account_query")]
        public ActionResult AddAccount()
        {
            return View("~/Views/System/v1/addAccount.cshtml");
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "account_edit")]
        public JsonResult AddAccount(AccountViewModel model)
        {
            BoolResult jsonResult = null;
            if (model.NoPrefixId.IsNotEmpty())
            {
                model.Item.Id = SecurityAppService.Corporation.Id + "_" + model.NoPrefixId;
            }
            BoolGenericResult<Account> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.LIST)]
        [ActionPermissionAttribute(Name = "account_query")]
        public JsonResult GetAccounts()
        {
            AccountJsonViewModel json = new AccountJsonViewModel();
            json.rows = new List<AccountJson>();

            IList<Account> accounts = this.AppService.GetBySearch(SearchFactory.CreateAccountSearch()).ToList();
            foreach (Account a in accounts)
            {
                AccountJson aj = Mapper.Map<Account, AccountJson>(a);
                json.rows.Add(aj);
            }
            json.total = accounts.Count;

            return Json(json, "application/json");
        }


        [ActionName(ActionConst.SEARCH)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "account_query")]
        public ActionResult GetAccountsBySearch()
        {
            this.Model.Purpose = RouteData.Values["purpose"].ToString();
            return PartialView("~/Views/System/v1/partial/selectAccountPartial.cshtml", this.Model);
        }


        [ActionName(ActionConst.SEARCH)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "account_query")]
        public JsonResult GetAccountsBySearch(AccountViewModel model)
        {
            AccountJsonViewModel json = new AccountJsonViewModel();
            json.rows = new List<AccountJson>();

            IList<Account> accounts = this.AppService.GetBySearch(new AccountSearch() { Name = model.SearchParam.Name }).ToList();
            foreach (Account a in accounts)
            {
                AccountJson aj = Mapper.Map<Account, AccountJson>(a);
                json.rows.Add(aj);
            }
            json.total = accounts.Count;

            return Json(json, "application/json");
        }

        [ActionName(ActionConst.INITIAL)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "account_init")]
        public ActionResult AddAccountInitial()
        {
            return View("~/Views/System/v1/addAccountInitial.cshtml");
        }

        [ActionName(ActionConst.INITIAL)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "account_init")]
        public JsonResult AddAccountInitial(string updated)
        {
            BoolGenericResult<Account> result = null;
            List<AccountJson> accountInitials = JsonConvert.DeserializeObject<List<AccountJson>>(updated);

            foreach (AccountJson json in accountInitials)
            {
                Account ai = this.AppService.GetById(json.Id);
                ai.InitialDate = DateTime.Parse(json.InitialDate);
                ai.InitialAmount = json.InitialAmount;
                result = this.AppService.Update(ai);
            }

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.DELETE)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "account_edit")]
        public JsonResult DeleteAccount(string selected)
        {
            BoolGenericResult<Account> result = null;
            List<AccountJson> accounts = JsonConvert.DeserializeObject<List<AccountJson>>(selected);

            foreach (AccountJson json in accounts)
            {
                Account ai = this.AppService.GetById(json.Id);
                result = this.AppService.Delete(ai);
            }

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }
    }
}
